package com.controller;

import com.pojo.GroupList;
import com.service.GroupListService;
import com.util.ServerResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * Created by Administrator on 2020/3/20.
 */
@Api(tags = "团购API")
@Controller
public class GroupListController {
    @Autowired
    private GroupListService groupListService;
    @ApiOperation(value ="添加团购",notes ="" )
    @PostMapping("/groups")
    @ResponseBody
    public ServerResponse add(GroupList groups){
        // ServerResponse serverResponse =
        //System.out.println("serverResponse:"+serverResponse);
        return groupListService.save(groups);
    }
    @ApiOperation(value = "删除团购",notes ="修改状态")
    @DeleteMapping("/groups/{id}")
    @ResponseBody
    public ServerResponse del(@PathVariable("id")String id){
        ServerResponse serverResponse = groupListService.modifyStatus(Integer.valueOf(id));
        System.out.println("serverResponse:"+serverResponse);
        return serverResponse;
    }

    @ApiOperation(value ="修改团购",notes ="应该用不到" )
    @PutMapping("/groups")
    @ResponseBody
    public ServerResponse upd(GroupList groups){
        System.out.println(groups);
        ServerResponse serverResponse = groupListService.modify(groups);
        return serverResponse;
    }
   /* @ApiOperation(value ="根据团购id查询")
    @GetMapping("/groups/{id}")
    @ResponseBody
    public ServerResponse findById(@PathVariable("id") String id){
        System.out.println(id);
        ServerResponse serverResponse = groupListService.getById(Integer.valueOf(id));
        return serverResponse;
    }*/

    @ApiOperation(value ="查询团购信息")
    @GetMapping("/groups")
    @ResponseBody
    public ServerResponse findAll(){
        ServerResponse serverResponse = groupListService.getAll();
        return serverResponse;
    }

    @ApiOperation(value = "获得团购的主页面",notes = "团购端")
    @GetMapping("/groups/backhome")
    public String goUserBackHome(Model model){
        ServerResponse serverResponse = groupListService.getAll();
            model.addAttribute("info",serverResponse);
        return "back/group/groupbackhome";
    }
    @ApiOperation(value = "团购列表的接口",notes = "")
    @GetMapping("/groups/list")
    @ResponseBody
    public ServerResponse  selectAll(){
        return groupListService.getAll();
    }

    @ApiOperation(value = "团购详情的接口",notes = "")
    @GetMapping("/groups/detail/{id}")
    @ResponseBody
    public ServerResponse  detail(@PathVariable("id") String groupId){
        return groupListService.getById(Integer.valueOf(groupId));
    }

    @ApiOperation(value = "首页秒杀接口",notes = "")
    @GetMapping("/groups/sale")
    @ResponseBody
    public ServerResponse  sale(String type){
        return groupListService.sale(type);
    }
    @ApiOperation(value = "获取七牛云token",notes = "")
    @GetMapping("/groups/token")
    @ResponseBody
    public String getQiniuToken() {
        return groupListService.getQiniuToken();
    }
    @GetMapping("/group/all")
    @ResponseBody
    public ServerResponse getall(String groupKeyword,String groupFuzzy,String other1,String groupPrice){
        return groupListService.getAllBySearch(groupKeyword,groupFuzzy,other1,groupPrice);
    }

    @ApiOperation(value ="首页点击搜索按钮的接口")
    @GetMapping("/search")
    @ResponseBody
    public ServerResponse searchByKW(String kw){
        ServerResponse serverResponse = groupListService.getBySearch(kw);
        if (serverResponse.getStatus() == 1){
            return serverResponse;
        }
        else
            return ServerResponse.getDataFailed("没有找到对应的店铺");
    }

}
